/**
 * 
 */
package com.cuc.platform.core.componet.security.service.logic;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.cuc.platform.core.componet.security.dao.SecurityResourceDao;
import com.cuc.platform.core.componet.security.domain.SecurityResource;
import com.cuc.platform.core.componet.security.service.SecurityResourceService;
import com.cuc.platform.core.service.ServiceException;

/**
 * @author peepul
 * 
 * 资源管理
 */
@Service("SecurityResourceService")
public class SecurityResourceServiceImpl implements SecurityResourceService {

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityResourceService#getAllRescs()
	 */
	@Override
	public List<SecurityResource> getAllRescs() {
		return securityResourceDao.getAllRescs();
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityResourceService#getRescById(java.lang.String)
	 */
	@Override
	public SecurityResource getRescById(Long id) {
		if (null == id) return null;
		
		return securityResourceDao.getRescById(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityResourceService#updateResc(com.cuc.platform.core.componet.security.domain.SecurityResource)
	 */
	@Override
	public void updateResc(SecurityResource resc) {
		if (null == resc) return;
		
		securityResourceDao.updateResc(resc);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityResourceService#addResc(com.cuc.platform.core.componet.security.domain.SecurityResource)
	 */
	@Override
	public void addResc(SecurityResource resc) throws ServiceException {
		if (null == resc) return;
		
		//验证priority值在数据库中是否已经存在
		Integer priority = resc.getPriority();
		if (null != priority){
			Integer rv = securityResourceDao.existPriority(priority);
			if (rv != 0){
				logger.warn("输入的priority：" + priority + "在数据库中已经存在。");
				throw new ServiceException("系统中已经存在输入的拦截序号，请换一个重新输入");
			}
		}
		
		//生成默认值
		String res_type = resc.getRes_type();
		if((null == res_type) || res_type.equals("")){
			resc.setRes_type("URL");//URL或METHOD是spring security可以识别的资源串
		}
		
		securityResourceDao.insertResc(resc);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityResourceService#delResc(java.lang.String)
	 */
	@Override
	public void delResc(Long id) throws ServiceException {
		if (null == id) return;
		
		int rv = securityResourceDao.delRescById(id);
		
		//如果返回值为0,则没找到对应id的数据，抛出业务异常给web层
		if(rv == 0){
			logger.warn("没找到id为：" + id + "的数据，无效的删除操作。");
			throw new ServiceException("系统中不存在要删除的数据！");
		}
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityResourceService#delMultiRescs(java.lang.String[])
	 */
	@Override
	public void delMultiRescs(String ... idArray) throws ServiceException {
		if ((null == idArray) || (idArray.length == 0)) return;
		
		securityResourceDao.delMultiResc(idArray);
	}

	//日志
	private static Logger logger = Logger.getLogger(SecurityResourceServiceImpl.class);
	
	/**
	 * Ioc Beans
	 */
	@Resource
	private SecurityResourceDao securityResourceDao;
}
